/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import dto.Account;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import utils.DBUtils;

/**
 *
 * @author trinhnta
 */
public class AccountDAO {
    public Account checkLogin(String username,String password){
        Connection con = null;
        CallableStatement cal = null;
        ResultSet rs = null;
        try {
            con = DBUtils.makeConnection();
            cal = con.prepareCall("{call checkLogin(?,?)}");
            cal.setString(1, username);
            cal.setString(2, password);
            rs = cal.executeQuery();
            Account account = null;
            if (rs.next()) {
                String name = rs.getString("Username");
                String pass = rs.getString("Password");
                String email = rs.getString("Email");
                String role = rs.getString("Role");
                account = new Account(name, pass, email, role);
            }
            return account;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if(cal != null){
                    cal.close();
                }
                if(con != null){
                    con.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        return null;
    }
}
